using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using TagLib;
using MyClever.Lib;

namespace MyClever.DefaultPlugins
{
    public class ReadAudioTags : MissionPlugin
    {
        [Package(Direction.Input, "Defines the audio files, you like to work with.")]
        enum AudioFiles
        {
            [Column(typeof(string), "The file, where you like to read the audio tags.",
                Validation="isexistingfile")]
            FilePath
        }

        [Package(Direction.Output, "Defines the given audio tags..")]
        enum AudioTags
        {
            [Column(typeof(string), "The location of the audio file.")]
            Location,
            [Column(typeof(string), "The title of the audio file.")]
            Title,
            [Column(typeof(string), "The album name of the audio file.")]
            Album,
            [Column(typeof(string), "The artist of the audio file.")]
            Artists,
            [Column(typeof(string), "Comment on the track.")]
            Annotation,
            [Column(typeof(string), "The time to render a resource, in milliseconds.")]
            Duration,
            [Column(typeof(string), "URI of an image of the track.")]
            Image,
            [Column(typeof(string), "URI of a place where this resource can be bought or more info can be found.")]
            Info
        }

        [Plugin("1.0.0.0", PluginDevStatus.Development, "Reads the basic and advanced tagging information stored in popular audio formats.")]
        public ReadAudioTags()
        {
        }

        public override void Initialize()
        {
            
            //This is the first Function called by the host...
            //Put anything needed to start with here first
        }

        public override void Dispose()
        {
            //Put any cleanup code in here for when the program is stopped
        }

        /// <summary>
        /// Generate new Source-content.
        /// </summary>
        protected override void Work()
        {
            DataTableReader audioFiles = this.GetDataReader(typeof(AudioFiles));
            while (audioFiles.Read())
            {
                this.AddNewLoggingMessage(typeof(AudioFiles), String.Format("read tags from:{0}", audioFiles[AudioFiles.FilePath.ToString()].ToString()));
                
                TagLib.File file = TagLib.File.Create (audioFiles[AudioFiles.FilePath.ToString()].ToString());
   
                DataRow newRow = this.CreateNewRowFromPackage(typeof(AudioTags));
                newRow[AudioTags.Location.ToString()] = audioFiles[AudioFiles.FilePath.ToString()].ToString();
                newRow[AudioTags.Title.ToString()] = file.Tag.Title;
                newRow[AudioTags.Album.ToString()] = file.Tag.Album;

                string performers = "";
                foreach (string performerOn in file.Tag.Performers)
                {
                    performers += performerOn + ";";
                }
                performers = performers.Remove(performers.Length - 1);
                newRow[AudioTags.Artists.ToString()] = performers;

                newRow[AudioTags.Annotation.ToString()] = file.Tag.Copyright;
                newRow[AudioTags.Duration.ToString()] = file.Length;

                string picture = "";
                if (file.Tag.Pictures.Length > 0)
                {
                    picture = file.Tag.Pictures[0].ToString();
                }
                newRow[AudioTags.Image.ToString()] = picture;

                newRow[AudioTags.Info.ToString()] = file.Tag.AmazonId;
                this.AddNewRowToPackage(typeof(AudioTags), newRow);
            }
        }
    }
}
